import { DataTypes, Model } from 'sequelize'
import { sequelize } from '../../connect'
import { findAllValues, findOneRandomValue, findOneValue } from '../../public'

type ModelProps = {
  id: number
  uid: number // 用户uid
  item_name: string // 物品名
  count: number // 数量
  attr: string // 额外属性，给装备等设定（可为json字符串或描述）
}

class bag extends Model<ModelProps> {
  public static findAllValues = findAllValues
  public static findOneValue = findOneValue
  public static findOneRandomValue = findOneRandomValue
}

bag.init(
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
      comment: '表id'
    },
    uid: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: '用户uid'
    },
    item_name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '物品名'
    },
    count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: '数量'
    },
    attr: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '物品额外属性'
    }
  },
  {
    sequelize,
    tableName: 'bag',
    freezeTableName: true,
    createdAt: false,
    updatedAt: false,
    indexes: [
      {
        unique: false,
        fields: ['id']
      }
    ]
  }
)

export { bag }
